package com.htthorizon.system.task;

import com.htthorizon.common.redis.service.RedisService;
import com.htthorizon.common.security.auth.AuthLogic;
import com.htthorizon.system.api.domain.SysLogininfor;
import com.htthorizon.system.api.domain.SysOperLog;
import com.htthorizon.system.service.ISysLogininforService;
import com.htthorizon.system.service.ISysOperLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;

/**
 * @author Brilliant
 */
@Component
@Slf4j
public class LogTask {

    @Autowired
    private RedisService redisService;

    @Autowired
    private ISysLogininforService sysLogininforService;

    @Autowired
    private ISysOperLogService sysOperLogService;
    @Scheduled(cron = "*/10 * * * * *")
    public void task(){
        Collection<String> keys = redisService.keys("log:*");
        List<SysOperLog> list = AuthLogic.list;

        for (int i=0;i<list.size();i++){
            sysOperLogService.insertOperlog(list.get(i));
            list.remove(i);
        }

        log.info("写入错误日志开始");
        for (String key: keys){
            SysOperLog cacheObject = redisService.getCacheObject(key);
            System.out.println(cacheObject);
            sysOperLogService.insertOperlog(cacheObject);
            redisService.deleteObject(key);
        }
        log.info("写入错误日志结束");
    }


}
